<template>
  <div style="width: 100%">
    <a-card class="card" :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="所属院系">
                <a-select allowClear :disabled="selectMenu.yx.length === 1" showSearch :filterOption="filterOption" v-model="queryParam.ssyxm">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="姓名">
                <a-input placeholder="请输入..." v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="工作单位">
                <a-input placeholder="请输入..." v-model="queryParam.gzdw"/>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:24}" :xl="{span: 24 }" style="text-align:center">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button type="primary" v-hasPermission="'dbwyxx:add'" @click="handleOpenEdit">新增</a-button>
              <a-button type="primary" v-hasPermission="'dbwyxx:delete'" @click="batchDelete">删除</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>

    <a-card class="card" :bordered="false">
      <STable
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :scroll="{ x: 1000 }"
      >
        <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
        <a slot="operation" v-hasPermission="'dbwyxx:update'" slot-scope="text, record" @click="handleOpenDetail(record.id)">修改</a>
      </STable>
    </a-card>

    <memberEdit
      v-if="initDone"
      ref="member"
      :editVisible="visible"
      @close="visible = false"
      @success="handleMemberSuccess"
      :yxList="selectMenu.yx"
      :data.sync="currentMember"
    />
  </div>
</template>

<script>
import { STable } from "~";
import memberEdit from "./memberEdit";

export default {
  components: {
    STable,
    memberEdit,
  },
  data() {
    return {
      queryParam: {},
      columns: [
        {
          title: "序号",
          scopedSlots: { customRender: "serial" },
          width: '100px'
        },
        {
          title: "所属院系",
          dataIndex: 'ssyxmc',
          width: '30%',
        },
        {
          title: "姓名",
          dataIndex: 'xm'
        },
        {
          title: "工作单位",
          dataIndex: 'gzdw',
          width: '30%',
        },
        {
          title: '操作',
          scopedSlots: { customRender: 'operation' },
          fixed:'right',
          width:100
        }
      ],
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = [];
          },
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange,
        },
      },
      selectedRowKeys: [],
      visible: false,
      loadData: (parameter) => {
        this.$nextTick(() => {
          this.selectedRowKeys = [];
          this.$refs.table.clearSelected();
        });
        parameter.pageNum = parameter.pageNo;
        let params = Object.assign(this.queryParam, parameter);
        return this.$api.degree.pleaWyList(params).then((res) => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total,
          };
        });
      },
      currentMember:{},
      selectMenu: {yx: []},
      initDone: false
    };
  },
  created () {
    this.$api.base.collegeAll().then(res => {
      this.selectMenu.yx = res.data
      if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
        this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
      }
      this.initDone = true
    })
  },
  methods: {
    handleOpenDetail(id) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.member.form.resetFields()
      })
      console.log(this.$nextTick())
      this.$api.degree.pleaWyInfo(id).then(res => {
        this.currentMember = res.data
      })
    },
    // 保存或者新建
    handleMemberSuccess(params) {
      const api = params.id ? 'pleaWyUpdate' :'pleaWyAdd'
      this.$api.degree[api](params).then(res => {
        this.$message.success(res.msg);
        this.visible = false;
        this.$refs.table.refresh(false)
      }).catch(err => {
        this.$message.error(res.msg);
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    //添加
    handleOpenEdit(){
      this.currentMember = [];
      this.visible = true
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        return this.$message.warning('请选择需要删除的委员')
      }
      this.$confirm({
        title: '确定删除选中的委员吗?',
        onOk: ()=> {
          this.$api.degree.pleaWyDelete(this.selectedRowKeys).then(res => {
            this.$message.success("操作成功")
            this.$refs.table.refresh(false)
            this.selectedRowKeys = []
          })
        },
        onCancel() {
        }
      });
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
    },
  },
};
</script>

<style>
</style>